package com.zjw.concurrency.aqs;

import java.util.PriorityQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionExample2 {

    static ReentrantLock reentrantLock = new ReentrantLock();
    static Condition full = reentrantLock.newCondition();
    static Condition empty = reentrantLock.newCondition();
    static final int queueSize = 10;
    static PriorityQueue<Integer> queue = new PriorityQueue<>(queueSize);

    public static void main(String[] args) {


        ConditionExample2 example2 = new ConditionExample2();
        Consumer consumer = example2.new Consumer();
        Producer producer = example2.new Producer();
        ;

        producer.start();
        consumer.start();
    }


//    class Consumer extends Thread {
//
//        @Override
//        public void run() {
//            consume();
//        }
//
//        private void consume() {
//            while (true) {
//                reentrantLock.lock();
//                try {
//                    while (queue.size() == 0) {
//                        System.out.println("队列空，等待数据");
//                        try {
//                            empty.await();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    queue.poll();
//                    full.signalAll();
//                    System.out.println("从队列里取走了一个数据，队列剩余" + queue.size() + "个元素");
//                } finally {
//                    reentrantLock.unlock();
//                }
//            }
//        }
//    }
//
//    class Producer extends Thread {
//
//        @Override
//        public void run() {
//            produce();
//        }
//
//        private void produce() {
//            while (true) {
//                reentrantLock.lock();
//                try {
//                    while (queue.size() == queueSize) {
//                        System.out.println("队列满，等待有空余");
//                        try {
//                            full.await();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    queue.offer(1);
//                    empty.signalAll();
//                    System.out.println("向队列插入了一个元素，队列剩余空间" + (queueSize - queue.size()));
//                } finally {
//                    reentrantLock.unlock();
//                }
//            }
//        }
//    }


    class Consumer extends Thread {

        @Override
        public void run() {
            this.consumer();
        }

        public void consumer() {
            while (true) {
                reentrantLock.lock();
                try {
                    while (queue.size() == 0) {
                        System.out.println("队列为空等待数据");
                        try {
                            empty.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //取出并消费数据，并唤醒生产者生产数据
                    queue.poll();
                    full.signalAll();
                    System.out.println("目前队列中有" + queue.size() + "个数据");
                } finally {
                    reentrantLock.unlock();
                }
            }

        }
    }


    class Producer extends Thread {

        @Override
        public void run() {
            this.producer();
        }

        public void producer() {
            while (true) {
                reentrantLock.lock();
                try {
                    while (queue.size() == queueSize) {
                        try {
                            System.out.println("队列已满，等待消费");
                            full.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //取出并消费数据，并唤醒生产者生产数据
                    queue.offer(1);
                    empty.signalAll();
                    System.out.println("向队列插入了一个元素，队列剩余空间" + (queueSize - queue.size()));
                } finally {
                    reentrantLock.unlock();
                }
            }

        }
    }
}
